package Sort.面试;

import java.time.DateTimeException;

/**
 * 输入某年某月某日，判断这一天是这一年的第几天？
 * （只允许用int 类型，不允许用date类型）
 */
public class DateIsYearThatDay {


    public static int getNumOfDayInYear(int year, int month, int day){
        int sum = 0;
        for(int i = 1; i <= month -1; i++){
            int temp = 0;
            switch(i){
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12: temp = 31; break;

                case 4:
                case 6:
                case 9:
                case 11: temp = 30; break;

                case 2:
                    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ){
                        temp = 29;
                        break;
                    }else{
                        temp = 28;
                        break;
                    }
                default:
                    return -1;
            }
            sum += temp;
        }
        sum += day;
        return sum;
    }

    public static int getWhichDayOfDate(int year, int month, int day){
        int sum = day;
        for(int i = 1; i < month; i++){
            switch(i){
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12: sum += 31; break;

                case 4:
                case 6:
                case 9:
                case 11: sum += 30; break;

                case 2:
                    if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)){
                        sum += 29;
                        break;
                    }else{
                        sum += 28;
                        break;
                    }
                default:
                    throw new IllegalArgumentException("请输入正确的日期");
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(getNumOfDayInYear(2008,3,1));
        System.out.println(getWhichDayOfDate(2008, 3, 1));
    }
}
